/**
 * Description: 客户端
 * File: client.c |Time: 2021-06-28 04:49
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "staff.h"
#include "client.h"


#define LISTEN_BACKLOG 10

int main(int argc, const char *argv[])
{
    if (argc < 3) {
        printf("Usage: %s <ip> <port>\n", argv[0]);
        return EXIT_FAILURE;
    }

    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    const char *ip = argv[1];
    const int port = atoi(argv[2]);

    if (socket < 0) {
        ERR("socket");
    }

    struct sockaddr_in serveraddr; 
    socklen_t addrlen = sizeof(serveraddr);
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(port);
    serveraddr.sin_addr.s_addr = inet_addr(ip);
    
	int input;
    int res;
    res = connect(sockfd, (struct sockaddr *)&serveraddr, addrlen);
    if (res < 0) {
        ERR("connect error");
    }
	
	//选择用户或者管理员登录
	while(1) {
		menu_login();
		scanf("%d",&input);
		getchar();
        input = 2;
		switch(input)
		{
		case 1:
			res = login(sockfd,TYPE_USER_LOGIN);
			if(res == 1)
			{
				user_menu();
			}else{
				break;
			}
			
		case 2:
			res = login(sockfd,TYPE_ADMIN_LOGIN);
			if(res)
 			{
				admin_menu(sockfd);
			}else{
				break;
			}
		case 3:
			printf("byebye\n");
			return 0;
		default:
			printf("您输入有误，请重新输入\n");
			break;
		}
		
		
	

		}

		


     /*   memset(buf, 0, sizeof(buf));
        printf("[to server] ");
        fgets(buf, BUFSIZE, stdin);
        buf[strlen(buf) - 1] = '\0';
        
        res = send(sockfd, buf, BUFSIZE, 0);
        if (res < 0) {
            ERR("send error");
        }
    
*/	
	
    
    return 0;
}




